package com.altekis.kanbankaze.client;

import com.allen_sauer.gwt.dnd.client.DragContext;
import com.allen_sauer.gwt.dnd.client.drop.AbsolutePositionDropController;
import com.altekis.kanbankaze.shared.Task;
import com.google.gwt.user.client.rpc.AsyncCallback;

final class StageDropController extends AbsolutePositionDropController {
	private final StageView stageColumn;
	
	private TaskServiceAsync taskService;

	StageDropController(StageView stageColumn, TaskServiceAsync taskService) {
		super(stageColumn);
		this.stageColumn = stageColumn;
		this.taskService = taskService;
	}

	@Override
	public void onDrop(DragContext context) {
		stageColumn.onDraggedTaskDrop(context);
		if (context.draggable instanceof TaskCardView) {
			TaskCardView taskView = (TaskCardView)context.draggable;
			Task task = taskView.getTask();
			task.onDrop(stageColumn.getStage());
			taskService.save(task, new AsyncCallback<Task>() {
				
				@Override
				public void onSuccess(Task result) {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public void onFailure(Throwable caught) {
					// TODO Auto-generated method stub
					
				}
			});
		}
		super.onDrop(context);
	}

	@Override
	public void onEnter(DragContext context) {
		stageColumn.onDraggedTaskEnter(context);
		super.onEnter(context);
	}

	@Override
	public void onLeave(DragContext context) {
		stageColumn.onDraggedTaskLeave(context);
		super.onLeave(context);
	}
}